<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\ArticleCategory;
use Illuminate\Http\Request;


class ArticleController extends Controller
{
    // 健康攻略分类列表
    public function category()
    {
        $list = ArticleCategory::orderByDesc('sort')->get();
        return $this->writeJson(0, $list);
    }

    // 健康攻略文章列表
    public function index(Request $request)
    {
        $model = Article::whereStatus(1);

        if ($cate_id = $request->post('cate_id')) {
            $model->where('cate_id', $cate_id);
        }

        $result = $model->paginate($this->limit);

        return $this->writeJson(0, $result->items());
    }

    // 健康攻略详情
    public function detail(Request $request)
    {
        $id = $request->post('id');
        if (empty($id)) {
            return $this->writeJson(500, null, '参数错误');
        }

        $article = Article::whereStatus(1)
            ->where('id', $id)
            ->first();
        if (empty($article)) {
            return $this->writeJson(500, null, '数据不存在');
        }

        $article->increment('view');

        return $this->writeJson(0, $article);
    }
}
